#pragma once

class M2InfoCache : public Singleton<M2InfoCache>
{
private:
	struct M2CacheEntry
	{
		i32 numrefs;
		M2Info* Info;
	};

public:
	M2Info& getInfo(const std::string& modelName) {
		MUTEX_LOCK(mCacheLock);
		if(mInfos.find(modelName) != mInfos.end()) {
			M2CacheEntry& me = mInfos[modelName];
			++me.numrefs;
			return *me.Info;
		}
		
		M2CacheEntry ent = {
			1,
			new M2Info(modelName)
		};

		mInfos[modelName] = ent;
		return *ent.Info;
	}

	void releaseInfo(const std::string& modelName) {
		MUTEX_LOCK(mCacheLock);
		if(mInfos.find(modelName) != mInfos.end()) {
			M2CacheEntry& me = mInfos[modelName];
			--me.numrefs;
			if(me.numrefs <= 0) {
				delete me.Info;
				mInfos.erase(modelName);
				return;
			}
		}
	}

private:
	std::map<std::string, M2CacheEntry> mInfos;
	Mutex mCacheLock;
};

#define sM2Info (*M2InfoCache::GetInstance())